package com.ruoyi.project.newhope.warning.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.newhope.audio.domain.NewHopeAudio;
import com.ruoyi.project.newhope.audio.mapper.NewHopeAudioMapper;
import com.ruoyi.project.newhope.device.domain.NewHopeInfraredDevice;
import com.ruoyi.project.newhope.device.mapper.NewHopeInfraredDeviceMapper;
import com.ruoyi.project.newhope.inspection.domain.NewHopeInspection;
import com.ruoyi.project.newhope.inspection.mapper.NewHopeInspectionMapper;
import com.ruoyi.project.newhope.material.domain.NewHopeMaterial;
import com.ruoyi.project.newhope.material.mapper.NewHopeMaterialMapper;
import com.ruoyi.project.newhope.materialInspection.domain.NewHopeMaterialInspection;
import com.ruoyi.project.newhope.materialInspection.mapper.NewHopeMaterialInspectionMapper;
import com.ruoyi.project.newhope.warning.domain.NewHopeWarning;
import com.ruoyi.project.newhope.warning.mapper.NewHopeWarningMapper;
import com.ruoyi.project.newhope.warning.service.INewHopeWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-02-08
 */
@Service
public class NewHopeWarningServiceImpl implements INewHopeWarningService
{
    @Autowired
    private NewHopeWarningMapper newHopeWarningMapper;
    @Autowired
    private NewHopeInfraredDeviceMapper newHopeInfraredDeviceMapper;
    @Autowired
    private NewHopeMaterialMapper newHopeMaterialMapper;
    @Autowired
    private NewHopeMaterialInspectionMapper newHopeMaterialInspectionMapper;
    @Autowired
    private NewHopeInspectionMapper newHopeInspectionMapper;
    @Autowired
    private NewHopeAudioMapper newHopeAudioMapper;

    /**
     * 查询用户
     * 
     * @param id 用户ID
     * @return 用户
     */
    @Override
    public NewHopeWarning selectNewHopeWarningById(Long id)
    {
        NewHopeWarning warning = newHopeWarningMapper.selectNewHopeWarningById(id);
        if (warning.getInfraredDeviceId() != null) {
            NewHopeInfraredDevice newHopeInfraredDevice = newHopeInfraredDeviceMapper.selectNewHopeInfraredDeviceById(warning.getInfraredDeviceId());
            if (newHopeInfraredDevice != null) {
                warning.setInfraredDeviceLocation(newHopeInfraredDevice.getLocation());
                warning.setInfraredDeviceName(newHopeInfraredDevice.getDesc());
            }
        }
        if (warning.getMaterialId() != null) {
            NewHopeMaterial material = newHopeMaterialMapper.selectNewHopeMaterialById(warning.getMaterialId());
            if (material != null) {
                warning.setMaterialCode(material.getMaterialCode());
                warning.setMaterialName(material.getMaterialDesc());
            }
            NewHopeInspection inspection = newHopeInspectionMapper.selectNewHopeInspectionById(warning.getInspectionId());
            if (inspection != null) {
                warning.setInspectionCode(inspection.getInspectionCode());
                warning.setInspectionName(inspection.getInspectionDesc());
            }
            if (warning.getInspectionId() != null && warning.getMaterialId() != null) {
                NewHopeMaterialInspection materialInspection = newHopeMaterialInspectionMapper.selectNewHopeMaterialInspectionByMaterialCodeAndInspectionId(warning.getMaterialId(), warning.getInspectionId());
                if (materialInspection != null) {
                    warning.setMaxValue(materialInspection.getMax());
                    warning.setMinValue(materialInspection.getMin());
                }
            }
        }
        return warning;
    }

    /**
     * 查询用户列表
     * 
     * @param newHopeWarning 用户
     * @return 用户
     */
    @Override
    public List<NewHopeWarning> selectNewHopeWarningList(NewHopeWarning newHopeWarning)
    {
        List<NewHopeWarning> newHopeWarnings = newHopeWarningMapper.selectNewHopeWarningList(newHopeWarning);
        for (NewHopeWarning warning : newHopeWarnings) {
            if (warning.getInfraredDeviceId() != null) {
                NewHopeInfraredDevice newHopeInfraredDevice = newHopeInfraredDeviceMapper.selectNewHopeInfraredDeviceById(warning.getInfraredDeviceId());
                if (newHopeInfraredDevice != null) {
                    warning.setInfraredDeviceLocation(newHopeInfraredDevice.getLocation());
                    warning.setInfraredDeviceName(newHopeInfraredDevice.getDesc());
                }
            }
            if (warning.getMaterialId() != null) {
                NewHopeMaterial material = newHopeMaterialMapper.selectNewHopeMaterialById(warning.getMaterialId());
                if (material != null) {
                    warning.setMaterialCode(material.getMaterialCode());
                    warning.setMaterialName(material.getMaterialDesc());
                }
                NewHopeInspection inspection = newHopeInspectionMapper.selectNewHopeInspectionById(warning.getInspectionId());
                if (inspection != null) {
                    warning.setInspectionCode(inspection.getInspectionCode());
                    warning.setInspectionName(inspection.getInspectionDesc());
                }
                if (warning.getInspectionId() != null && warning.getMaterialId() != null) {
                    NewHopeMaterialInspection materialInspection = newHopeMaterialInspectionMapper.selectNewHopeMaterialInspectionByMaterialCodeAndInspectionId(warning.getMaterialId(), warning.getInspectionId());
                    if (materialInspection != null) {
                        warning.setMaxValue(materialInspection.getMax());
                        warning.setMinValue(materialInspection.getMin());
                    }
                }

            }
        }
        return newHopeWarnings;
    }

    /**
     * 新增用户
     * 
     * @param newHopeWarning 用户
     * @return 结果
     */
    @Override
    public int insertNewHopeWarning(NewHopeWarning newHopeWarning)
    {
        newHopeWarning.setCreateTime(DateUtils.getNowDate());
        return newHopeWarningMapper.insertNewHopeWarning(newHopeWarning);
    }

    /**
     * 修改用户
     * 
     * @param newHopeWarning 用户
     * @return 结果
     */
    @Override
    public int updateNewHopeWarning(NewHopeWarning newHopeWarning)
    {
        newHopeWarning.setUpdateTime(DateUtils.getNowDate());
        return newHopeWarningMapper.updateNewHopeWarning(newHopeWarning);
    }

    /**
     * 删除用户对象
     * 
     * @param id 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNewHopeWarningByIds(String id)
    {
        return newHopeWarningMapper.deleteNewHopeWarningByIds(Convert.toStrArray(id));
    }

    /**
     * 删除用户信息
     * 
     * @param id 用户ID
     * @return 结果
     */
    @Override
    public int deleteNewHopeWarningById(Long id)
    {
        return newHopeWarningMapper.deleteNewHopeWarningById(id);
    }

    @Override
    public NewHopeWarning selectNewHopeWarningOne() {
        NewHopeWarning warning = newHopeWarningMapper.selectNewHopeWarningOne();
        if (warning != null && warning.getInspectionId() != null) {
            NewHopeInspection inspection = newHopeInspectionMapper.selectNewHopeInspectionById(warning.getInspectionId());
            if (inspection != null && inspection.getAudioId() != null) {
                NewHopeAudio audio = newHopeAudioMapper.selectNewHopeAudioById(inspection.getAudioId());
                if (audio != null) {
                    warning.setAudio(audio);
                }
            }

            if (warning.getInfraredDeviceId() != null) {
                NewHopeInfraredDevice newHopeInfraredDevice = newHopeInfraredDeviceMapper.selectNewHopeInfraredDeviceById(warning.getInfraredDeviceId());
                if (newHopeInfraredDevice != null) {
                    warning.setInfraredDeviceLocation(newHopeInfraredDevice.getLocation());
                    warning.setInfraredDeviceName(newHopeInfraredDevice.getDesc());
                }
            }
            if (warning.getMaterialId() != null) {
                NewHopeMaterial material = newHopeMaterialMapper.selectNewHopeMaterialById(warning.getMaterialId());
                if (material != null) {
                    warning.setMaterialCode(material.getMaterialCode());
                    warning.setMaterialName(material.getMaterialDesc());
                }
                if (inspection != null) {
                    warning.setInspectionCode(inspection.getInspectionCode());
                    warning.setInspectionName(inspection.getInspectionDesc());
                }
                if (warning.getInspectionId() != null && warning.getMaterialId() != null) {
                    NewHopeMaterialInspection materialInspection = newHopeMaterialInspectionMapper.selectNewHopeMaterialInspectionByMaterialCodeAndInspectionId(warning.getMaterialId(), warning.getInspectionId());
                    if (materialInspection != null) {
                        warning.setMaxValue(materialInspection.getMax());
                        warning.setMinValue(materialInspection.getMin());
                    }
                }
            }
        }
        return warning;
    }

}
